<script>

    $(document).ready(function() {
        $('#btn_competition_best').click(function() {
           
            var min_year = $('#min_year').val();
            var max_year = $('#max_year').val();
            
            if (min_year > max_year) {
                var tmp = min_year;
                min_year = max_year;
                max_year = tmp;
            }
            
            var data = {
                min_year : min_year,
                max_year : max_year,
                ajax : 1
            }
           
            $.ajax({
                url: $(this).attr('href'),
                type: 'POST',
                data: data,
                success: function(result) {
                    $('#competition_best_fina').html(result);
                }
                  
            });
            return false;
        });
    });

</script>

<style>

    .form-group {
        margin-right: 5px;
        margin-left: 5px;
    }
    
</style>

<!-- <div id="competition_best_fina"> wrapper is around this content in views/competition.php file --> 
<?php
echo '<div class="form-inline">';
echo form_open('competitions/ajax_best_fina');
$now = date("Y");
for ($i = $min_year; $i <= $max_year; $i++) {
    //$age = $now - $i;
    $label = "$i sündinud";
    if ($i == $sel_min_year) {
        $min_selected[$i] = $i;
    }
    if ($i == $sel_max_year) {
        $max_selected[$i] = $i;
    }
    $min_options[$i] = $label;
    $max_options[$i] = $label;
}

echo '<div class="form-group">';
echo form_dropdown('min_year', $min_options, $min_selected, 'id="min_year" class="form-control"');
echo '</div> - <div class="form-group">';
echo form_dropdown('max_year', $max_options, $max_selected, 'id="max_year" class="form-control"');
echo '</div>';
echo form_submit('submit', 'Näita parimaid', 'id="btn_competition_best" class="btn btn-default"');
echo form_close();
?>
</div>
<div class="fragment" style="margin-top:20px; ">
    <table class="table">
        <caption><h4>Parimad mehed FINA punktide alusel <?php echo "($sel_min_year-$sel_max_year)"; ?></h4></caption>
        <thead>
            <tr>
                <th>Koht</th>
                <th>Ala</th>
                <th>Nimi</th>
                <th>Sünnia</th>
                <th>Tulemus</th>
                <th>Klubi</th>
                <th>FINA</th>
            </tr>
        </thead>
        </tbody>
        <?php
        $rank = 1;
        foreach ($best_results_m as $row) {
            echo '<tr>';
            echo "<td>$rank</td>";
            echo "<td>$row->distance" . ' ' . Utils::est_stroke($row->stroke, false, true) . '</td>';
            echo '<td><a href="' . site_url('athletes/profile/' . $row->athlete_id) . '">' . $row->first_name . ' ' . $row->last_name . '</a></td>';
            echo '<td>' . $row->birthyear . '</td>';
            echo '<td>' . $this->Utils->ms_to_result_string($row->result_ms) . '</td>';
            echo '<td>' . $row->team . '</td>';
            echo '<td>' . $row->fina . '<td>';
            echo '</tr>';
            $rank++;
        }
        ?>
        </tbody>
    </table>
</div>
<div class="fragment" style="margin-top:20px; ">
    <table class="table">
        <caption><h4>Parimad naised FINA punktide alusel  <?php echo "($sel_min_year-$sel_max_year)"; ?></h4></caption>
        <thead>
            <tr>
                <th>Koht</th>
                <th>Ala</th>
                <th>Nimi</th>
                <th>Sünnia</th>
                <th>Tulemus</th>
                <th>Klubi</th>
                <th>FINA</th>
            </tr>
        </thead>
        </tbody>
        <?php
        $rank = 1;
        foreach ($best_results_f as $row) {
            echo '<tr>';
            echo "<td>$rank</td>";
            echo "<td>$row->distance" . ' ' . Utils::est_stroke($row->stroke, false, true) . '</td>';
            echo '<td><a href="' . site_url('athletes/profile/' . $row->athlete_id) . '">' . $row->first_name . ' ' . $row->last_name . '</a></td>';
            echo '<td>' . $row->birthyear . '</td>';
            echo '<td>' . $this->Utils->ms_to_result_string($row->result_ms) . '</td>';
            echo '<td>' . $row->team . '</td>';
            echo '<td>' . $row->fina . '<td>';
            echo '</tr>';
            $rank++;
        }
        ?>
        </tbody>
    </table>
</div>